/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Canvas;

/**
 * Generic definition for widgets that can be interacted with.
 * 
 * @author kentelt
 * 
 */
public abstract class TraversableWidget extends Widget {
	protected boolean isInFocus;
	protected ClickListener clikListenr;

	public void setClickListener(ClickListener listener) {
		this.clikListenr = listener;
	}

	/**
	 * 
	 * @param width
	 *            of the widget
	 * @param height
	 *            of the widget
	 */
	protected TraversableWidget(Theme theme, int width, int height) {
		super(theme, width, height);
	}

	public void fire() {
		if (clikListenr != null) {
			clikListenr.clickPerformed(this);
		}
	}

	protected void keyPressed(int keyCode) {
		// System.out.println("key pressed in traversal widget");
		keyPressed(getGameAction(keyCode), keyCode);
	}

	public boolean keyPressed(int gameActionCode, int key) {
		if (isEnabled) {
			switch (gameActionCode) {
			case Canvas.LEFT:
				return moveLeft();
			case Canvas.RIGHT:
				return moveRight();
			case Canvas.DOWN:
				return moveDown();
			case Canvas.UP:
				return moveUp();
			case Canvas.FIRE:
				// System.out.println("fire key in traversal widget");
				fire();
				return true;
			}
		} else {
			isInFocus = false;

		}
		return false;
	}

	public boolean moveDown() {
		isInFocus = false;
		return false;
	}

	public boolean moveLeft() {
		isInFocus = false;
		return false;
	}

	public boolean moveRight() {
		isInFocus = false;
		return false;
	}

	public boolean moveUp() {
		isInFocus = false;
		return false;
	}

	/**
	 * Widgets should implement pointerDragged operations in this method
	 * 
	 * @param x
	 * @param y
	 * @return True if traversal took place within the widget, False if the
	 *         click was not within the widget or if no traversal took place in
	 *         the widget or if the widget is not visible
	 */
	public boolean pointerDrag(int x, int y) {
		return false;
	}

	/**
	 * Cannot be overriden by subclasses.
	 * <p>
	 * sub classes must use pointerDrag
	 * </p>
	 */
	protected final void pointerDragged(final int x, final int y) {
		pointerDrag(x, y);
	}

	/**
	 * Widgets should implement pointerPressed operations in this method
	 * 
	 * @param px
	 * @param py
	 * @return True if traversal took place within the widget, False if the
	 *         click was not within the widget or if no traversal took place in
	 *         the widget or if the widget is not visible
	 */
	public boolean pointerPress(int px, int py) {
		if (isVisible && com.aphrodite.util.geom.GeomUtil.withinBounds(px, py, x, y, width, height + titleHeight)) {
			/* check if the click is on the title */
			return (py - y < titleHeight) ? false : true;
		}
		return false;
	}

	/**
	 * Cannot be overriden by subclasses.
	 * <p>
	 * sub classes must use pointerPress
	 * </p>
	 */
	protected final void pointerPressed(final int x, final int y) {
		pointerPress(x, y);
	}

	/**
	 * Widgets should implement pointerReleased operations in this method
	 * 
	 * @param x
	 * @param y
	 * @return True if traversal took place within the widget, False if the
	 *         click was not within the widget or if no traversal took place in
	 *         the widget or if the widget is not visible
	 */
	public boolean pointerRelease(final int x, final int y) {
		return false;
	}

	/**
	 * Cannot be overriden by subclasses.
	 * <p>
	 * sub classes must use pointerRelease
	 * </p>
	 */
	protected final void pointerReleased(final int x, final int y) {
		pointerRelease(x, y);
	}

	protected void removeFocus() {
		isInFocus = false;
	}

	protected void setFocus() {
		isInFocus = true;
		System.out.println("Focus gained");
	}

	abstract protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout);
	// {
	// switch (dir) {
	// // case Canvas.DOWN:
	// if (dir == Canvas.DOWN || dir == Canvas.RIGHT) {
	// if (status == UPPER) {
	// System.out.println("Focus gained");
	// status = IN;
	// } else {
	// status = LOWER;
	// System.out.println("Focus lost");
	//
	// return false;
	// }
	// }
	// // break;
	// // case Canvas.UP:
	// else if (dir == Canvas.UP || dir == Canvas.LEFT) {
	// if (status == LOWER) {
	// System.out.println("Focus gained");
	// status = IN;
	// } else {
	// status = UPPER;
	// System.out.println("Focus lost");
	// return false;
	// }
	// // break;
	// }
	// return true;
	// }
}
